using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GAS.Common;
using GAS.Common.DataSets;
using GAS.Common.Managers;
using GAS.Common.Constants;
using GAS.GASWebServices;

namespace GAS
{
    public partial class AuditOptionsView : Form
    {
		private GASManager gasManager;
		private GASWebService webService;
		bool webServiceConnection;


		public AuditOptionsView(bool webServiceConnection_)
        {
            InitializeComponent();
			
			comboAuditType.DataSource = GASConstants.auditTypes;
			comboAuditType.SelectedIndex = 0;

			webServiceConnection = webServiceConnection_;
			if (webServiceConnection)
				webService = new GASWebService();
			else
				gasManager = new GASManager();

        }

		private void buttonGenerateNewAudit_Click(object sender, EventArgs e)
		{
			AuditDataSet auditDS;
			AuditDataSet.GAS_AUDITRow auditRow;
			AuditDataSet.GAS_AUDIT_TYPESRow auditTypeRow;
			List<string> auditOptions;

			auditDS = new AuditDataSet();

			// AUDIT
			auditRow = auditDS.GAS_AUDIT.NewGAS_AUDITRow();
			auditRow.AUDIT_DATE = dateTimePickerAuditDate.Value;
			auditRow.USER_NAME = textAuditorName.Text;
			auditRow.GUID = System.Guid.NewGuid().ToString();
			auditDS.GAS_AUDIT.AddGAS_AUDITRow(auditRow);

			// AUDIT_TYPES
			auditOptions = GetAuditOptions();
			foreach (string auditOption in auditOptions)
			{
				auditTypeRow = auditDS.GAS_AUDIT_TYPES.NewGAS_AUDIT_TYPESRow();
				auditTypeRow.AUDIT_ID = auditRow.ID;
				auditTypeRow.AUDIT_TYPE = auditOption;
				auditDS.GAS_AUDIT_TYPES.AddGAS_AUDIT_TYPESRow(auditTypeRow);
			}

			if (webServiceConnection)
				webService.SaveAudit(auditDS);
			else
				gasManager.SaveAudit(auditDS);
		}

		private List<string> GetAuditOptions()
		{
			List<string> auditOptions;

			auditOptions = new List<string>();

			if (checkBoxUser.Checked)
				auditOptions.Add(GASConstants.GAS_AUDIT_OPTION_USERS_AND_GROUPS);
			if (checkBoxSecuritySettings.Checked)
				auditOptions.Add(GASConstants.GAS_AUDIT_OPTION_SECURITY_SETTINGS);
			if (checkBoxOpenPorts.Checked)
				auditOptions.Add(GASConstants.GAS_AUDIT_OPTION_OPEN_PORTS);
			if (checkBoxNetwork.Checked)
				auditOptions.Add(GASConstants.GAS_AUDIT_OPTION_NETWORK_ADAPTERS);
			if (checkBoxUpdates.Checked)
				auditOptions.Add(GASConstants.GAS_AUDIT_OPTION_UPDATES);
			if (checkBoxLogs.Checked)
				auditOptions.Add(GASConstants.GAS_AUDIT_OPTION_LOGS);
			if (checkBoxHardware.Checked)
				auditOptions.Add(GASConstants.GAS_AUDIT_OPTION_HARDWARE);
			if (checkBoxSoftware.Checked)
				auditOptions.Add(GASConstants.GAS_AUDIT_OPTION_SOFTWARE);
			if (checkBoxProcesses.Checked)
				auditOptions.Add(GASConstants.GAS_AUDIT_OPTION_PROCESSES);

			return auditOptions;
		}

		private void comboAuditType_SelectedIndexChanged(object sender, EventArgs e)
		{
			switch (comboAuditType.SelectedIndex)
			{
				case 0:
					checkBoxUser.Checked = true;
					checkBoxSecuritySettings.Checked = true;
					checkBoxOpenPorts.Checked = true;
					checkBoxNetwork.Checked = true;
					checkBoxUpdates.Checked = true;
					checkBoxLogs.Checked = true;
					checkBoxHardware.Checked = true;
					checkBoxSoftware.Checked = true;
					checkBoxProcesses.Checked = true;
					break;
				case 1:
					checkBoxUser.Checked = true;
					checkBoxSecuritySettings.Checked = true;
					checkBoxOpenPorts.Checked = true;
					checkBoxNetwork.Checked = true;
					checkBoxUpdates.Checked = true;
					checkBoxLogs.Checked = true;
					checkBoxHardware.Checked = true;
					checkBoxSoftware.Checked = true;
					checkBoxProcesses.Checked = true;
					break;
				case 2:
					checkBoxUser.Checked = true;
					checkBoxSecuritySettings.Checked = true;
					checkBoxOpenPorts.Checked = true;
					checkBoxNetwork.Checked = true;
					checkBoxUpdates.Checked = true;
					checkBoxLogs.Checked = false;
					checkBoxHardware.Checked = true;
					checkBoxSoftware.Checked = true;
					checkBoxProcesses.Checked = false;
					break;
			}
		}

    }
}